﻿using fuzhu;
using LH.Models.APAR;
using Microsoft.Extensions.Logging;
using NetTaste;
using Newtonsoft.Json;
using Org.BouncyCastle.Asn1.Ocsp;
using SqlSuarEntity;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using XXX.Models.GL;
using XXX.Models.U8Entity;
using XXX.Models.WDFT;
using ZDEntity;

namespace Yonyou.U8.BO
{
     //万吉生产计划，新增
    public class WanJiSCJHVM  
    {   
            

           
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public static string cDatabase = "";
        public zdy_wanji_jihua_ViewList lstscjh;
 

public RetModel2 Add()
    {
        try
        {
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {
                // 按日期分组
                var groupByDate = lstscjh.vouList.GroupBy(x => x.dDate.Date);

                foreach (var group in groupByDate)
                {
                    string dDateStr = group.Key.ToString("yyyyMMdd");
                    string sql2 = string.Format("select ISNULL(MAX(right(ccode,3)),0) from zdy_wanji_jihua where ccode like '{0}%' and len(ccode)=11", dDateStr);
                    int maxSerial = db.Ado.GetInt(sql2);

                    int serial = maxSerial + 1;
                    foreach (var item in group.OrderBy(x => x.dDate)) // 保证顺序
                    {
                        item.cCode = dDateStr + serial.ToString().PadLeft(3, '0');
                        serial++;
                    }
                }

                int i = db.Insertable<zdy_wanji_jihua>(lstscjh.vouList).ExecuteCommand();

                if (i > 0)
                {
                    return DBService.ReOKMsg("新增完成");
                }
                else
                {
                        return DBService.ReErrObject("新增失败");
                    }
            }
        }
        catch (Exception ex)
        {
            return DBService.ReErrObject(ex.Message);
        }
    }

}
}
